//
//  LogisticsInfoViewController.m
//  PPDL
//
//  Created by 王洋 on 2019/8/8.
//  Copyright © 2019 王洋. All rights reserved.
//
#import "LogisticsInfoCell.h"
#import "LogisticsInfoViewController.h"

@interface LogisticsInfoViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (nonatomic, strong) NSMutableArray<LogistcsModel *>* dataArray;
@end

@implementation LogisticsInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"物流信息";

    [self initView];
    [self request_LogisticsInfo];
}

- (void)request_LogisticsInfo {
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:self.orderID forKey:@"id"];
//    [dic setValue:@"1" forKey:@"curPage"];
//    [dic setValue:@"100" forKey:@"pageSize"];

    //传一个id  提示用户无此订单
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,TT_ORDER_LOGISTIC_LIST];
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        if ([data isKindOfClass:[NSDictionary class]]) {
            
            NSString * code = [data valueForKey:@"code"];
            if ([code isEqualToString:@"200"]) {
                
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
            
                if ([resultDic isKindOfClass:[NSDictionary class]]) {
                    
                    NSArray *resultArray = [resultDic valueForKey:@"list"];
                    if ([resultArray isKindOfClass:[NSArray class]]) {
                        
                        
                        if ([resultArray isKindOfClass:[NSArray class]]) {
                            
                            for (int i = 0; i < resultArray.count; i++) {
                                
                                NSDictionary *LogistcsDic = [resultArray objectAtIndex:i];
                                
                                LogistcsModel *model = [[LogistcsModel alloc] init];
                                model.logisticsStatus = [LogistcsDic valueForKey:@"content"];
                                model.logisticsTime = [LogistcsDic valueForKey:@"time"];
                                
                                if (i==0) {
                                    model.isLatest = YES;
                                    model.isOldest = NO;
                                    model.isMiddle = NO;
                                } else if (i>0 && i<resultArray.count-1) {
                                    model.isLatest = NO;
                                    model.isOldest = NO;
                                    model.isMiddle = YES;
                                } else {
                                    model.isLatest = NO;
                                    model.isOldest = YES;
                                    model.isMiddle = NO;
                                }
                                
                                [self.dataArray addObject:model];
                            }
                            
                        }
                        
                    }
                    
                }
 
                [[TTNoResultManager shareManager] hideNoResultWithView:self.view];
                
            } else {
                
                //检查到未登录
                if ([code isEqualToString:@"401"]) {
                    [[TTUserModel sharedObject] initialization];

                    [self openLoginWithCompletion:^(BOOL isLogin, NSDictionary * _Nonnull options) {
                        if (isLogin) {
                            [self request_LogisticsInfo];
                        }
                    }];
                    
                } else {
                    [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:kTTViewStateNoData btnTitle:@"刷新试试" didPressButton:^{
                        [self request_LogisticsInfo];
                    }];
                    
                }
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"msg"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
        } else {
            
        }
        
        [self.tableview reloadData];
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        WEAKSELF
        [[TTNoResultManager shareManager] showNoResultWithStateInView:self.view errorCode:error.code btnTitle:@"重新加载" didPressButton:^{
            [weakSelf request_LogisticsInfo];
        }];
    }];
}

- (void)initView{
    
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    self.tableview.backgroundColor = PP_Default_Color_Gray_F1F1F1;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"LogisticsInfoCell" bundle:nil] forCellReuseIdentifier:@"LogisticsInfoCellID"];
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LogisticsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LogisticsInfoCellID" forIndexPath:indexPath];
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    cell.selectionStyle =UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}


- (NSMutableArray<LogistcsModel *> *)dataArray {
    
    if (!_dataArray) {
        _dataArray = @[].mutableCopy;
    }
    return _dataArray;
}
@end
